package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.ITagDao;       
import com.magicode.cms.entiy.base.Tag;
import com.magicode.cms.entiy.base.User;
import com.magicode.cms.entiy.base.UserSubscription;
import com.magicode.cms.service.ITagService;

@Service("servicetag")
public class TagServiceImpl extends BaseServiceImpl<Tag,ITagDao> implements ITagService,UtilPath{
	@Autowired
	@Qualifier("daoTag")
	private ITagDao tagDao;
	public ITagDao getDao() throws Exception {
		return tagDao;
	}
	public ITagDao getTagDao() {
		return tagDao;
	}

	public void setTagDao(ITagDao tagDao) {
		this.tagDao = tagDao;
	}
	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("initialWordDic", ""+entity_alias_name+".initialWordDic.title as initialWordDic ");
			gridsel.put("initialWordDic", ""+entity_alias_name+".initialWordDic.code as titlecode ");
			gridsel.put("initSubCount", ""+entity_alias_name+".initSubCount as initSubCount ");
			gridsel.put("realSubCount", ""+entity_alias_name+".initSubCount as realSubCount ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	/**
	 * 
	 */
	public List<Map<String,Object>> simpleSelect(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as text ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("initialWordDic", ""+entity_alias_name+".initialWordDic.title as initialWordDic ");
			gridsel.put("initSubCount", ""+entity_alias_name+".initSubCount as initSubCount ");
			gridsel.put("realSubCount", ""+entity_alias_name+".initSubCount as realSubCount ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Map<String, Object>>();
		}
		
	}
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql = " from "+com.magicode.cms.entiy.base.Tag.class.getName()+" "+entity_alias_name+" where 1=1 ";
		if(null!=map && !map.isEmpty()){
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id='"+map.get(field_id)+"'";
			}
			if(map.containsKey("initialWordDic")){
				String worldDicStr = map.get("initialWordDic");
				hql = hql+" and "+entity_alias_name+".initialWordDic.id='"+worldDicStr+"'";
			}
			if(map.containsKey("titleOrinitialWordDicCode")){
				String worldDicStr = map.get("titleOrinitialWordDicCode");
				hql = hql+" and ( "+entity_alias_name+".initialWordDic.code='"+worldDicStr+"' "
						+ " or "+entity_alias_name+".title like '%"+worldDicStr+"%' ) ";
			}
//			查询title
			if(map.containsKey("title")){
				hql = hql+" and "+entity_alias_name+".title like '%"+map.get("title")+"%'";
			}
		}
		return hql;
	}
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "名称");
		map.put("width", 250);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "initialWordDic");
		map.put(json_title, "首字母");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "initSubCount");
		map.put(json_title, "初始化订阅数量");
		map.put("width", 100);
		beaninfo.add(map);	
		map = new HashMap<String, Object>();
		map.put(json_key, "realSubCount");
		map.put(json_title, "实际订阅数量");
		map.put("width", 100);
		beaninfo.add(map);	
		map = new HashMap<String, Object>();
		map.put(json_key, "orderCode");
		map.put(json_title, "排序码");
		map.put("width", 100);
		beaninfo.add(map);	
		return beaninfo;
	}
	
}
